package com.bodystm.temperature.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bodystm.temperature.patient.PatientBedService;
import com.bodystm.temperature.patient.PatientService;

/**
 * 閲囬泦鍣ㄧ殑鏈嶅姟绔�
 * 
 * @author ggeagle
 *
 */
public class GatherServer implements Runnable {
	/**
	 * 鏃ュ織璁板綍鍣�
	 */
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private UrineBagService urineBagService;
	private PatientService patientService;
	private PatientBedService patientBedUserService;
	/**
	 * 鏈嶅姟鍣ㄧ绋嬪簭
	 */
	private ServerSocket serverSocket;
	public UrineBagService getUrineBagService() {
		return urineBagService;
	}

	public void setUrineBagService(UrineBagService urineBagService) {
		this.urineBagService = urineBagService;
	}

	public PatientService getPatientService() {
		return patientService;
	}

	public void setPatientService(PatientService patientService) {
		this.patientService = patientService;
	}

	public PatientBedService getPatientBedUserService() {
		return patientBedUserService;
	}

	public void setPatientBedUserService(PatientBedService patientBedUserService) {
		this.patientBedUserService = patientBedUserService;
	}

	private ExecutorService executorService;// 绾跨▼姹�
	private final int POOL_SIZE = 5000;// 鍗曚釜CPU绾跨▼姹犲ぇ灏�

	public GatherServer(int port) throws IOException {
		this.serverSocket = new ServerSocket(port);
		executorService = Executors.newFixedThreadPool(Runtime.getRuntime()
				.availableProcessors() * POOL_SIZE);
		logger.info("服务器启动");
	}

	@Override
	public void run() {
		while (true) {
			Socket socket = null;
			try {
				socket = serverSocket.accept();
				executorService.execute(new DataGather(socket,
						this.urineBagService,this.patientService,this.patientBedUserService));
			} catch (Exception e) {
				logger.error("接收客户端连接时，发生异常！", e);
			}
		}
	}
}
